package com.atguigu.gulimall.product.web;

import com.atguigu.gulimall.product.entity.Cat;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.service.Test;
import com.atguigu.gulimall.product.vo.Catalog2Level;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Controller
@EnableConfigurationProperties(Cat.class)
public class IndexController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private Test test;
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private Cat cat;

    @GetMapping(value = {"/","index.html"})
    public String index(Model model){
        List<CategoryEntity> categoryEntityList = categoryService.getCategoryLevel1();
        model.addAttribute("categories",categoryEntityList);
        return "index";
    }

    @ResponseBody
    @GetMapping(value = {"/index/catalog.json"})
    public Map<String,List<Catalog2Level>> catalogJson(){
        Map<String,List<Catalog2Level>> map = categoryService.getCatalogJson();
        return map;
        //
    }


    @ResponseBody
    @GetMapping("/hello")
    public String hello(){
        RLock myLock = redissonClient.getLock("myLock");
        myLock.lock(10,TimeUnit.SECONDS);
        try{
            System.out.println("加锁成功，执行业务");
            Thread.sleep(20000);
        }catch (Exception e){

        }finally {
            System.out.println("解锁成功");
            myLock.unlock();
        }
        return "hello";
    }

    @ResponseBody
    @GetMapping("/write")
    public String write(){
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        RLock rLock = readWriteLock.writeLock();
        try {
            rLock.lock();
            System.out.println("写锁加锁成功");
            String string = UUID.randomUUID().toString();
            stringRedisTemplate.opsForValue().set("mylock",string);
            Thread.sleep(20*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }
        return "write";
    }

    @ResponseBody
    @GetMapping("/read")
    public String read(){
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        RLock rLock = readWriteLock.readLock();
        String string = null;
        try {
            rLock.lock();
            System.out.println("读锁加锁成功");
            string = stringRedisTemplate.opsForValue().get("mylock");
            //Thread.sleep(20*1000);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }
        return string;
    }

    @ResponseBody
    @GetMapping("park")
    public String park(){
        RSemaphore park = redissonClient.getSemaphore("park");
        try {
            park.acquire();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "ok";
    }

    @ResponseBody
    @GetMapping("go")
    public String go(){
        RSemaphore park = redissonClient.getSemaphore("park");
        park.release();
        return "ok";
    }

    @ResponseBody
    @GetMapping("/lockDoor")
    public String lockDoor(){
        RCountDownLatch lockDoor = redissonClient.getCountDownLatch("lockDoor");
        lockDoor.trySetCount(5L);
        try {
            lockDoor.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "放假了,可以锁门了";
    }

    @ResponseBody
    @GetMapping("/{id}/go")
    public String gogogo(@PathVariable Long id){
        RCountDownLatch lockDoor = redissonClient.getCountDownLatch("lockDoor");
        lockDoor.countDown();
        return id+"班学生放假";
    }

    @ResponseBody
    @GetMapping("/testConfigurationProperties")
    public String testConfigurationProperties(){
        return cat.toString();
    }
}
